/* 

		SVN 
		$LastChangedRevision: 12 $
		$LastChangedDate: 2013-10-14 14:38:18 +0000 (Mon, 14 Oct 2013) $
		$LastChangedBy: picciux@gmail.com $
		 
*/

/******************************* GLOBS *********************************/
var ImGallery = {
	feedType: {
		PICASA: 1,
		RSS: 2
	},
		
	prefs: {
		FEED_TYPE: "feedType",
		USER_ID: "userid",
		SLIDESHOW: "slideshow",
		SLIDE_INTERVAL: "ssinterval",
		SHOW_CAPTION: "showCaption",
		AUTO_HEIGHT: "autoHeight"
	},
	
	picasa: {
		prefs: {
			ALBUM_ID: "picasaAlbumId",
			AUTH_KEY: "picasaAuthKey"			
		},
		
		BASE_URL: "https://picasaweb.google.com/data/feed/api/user/",
		
		listAlbumsURL: function(userid, authkey) {
			if (authkey != null && authkey.length > 0)
				authkey = "&authkey=" + authkey;
			else
				authkey = "";
			
			return ImGallery.picasa.BASE_URL + 
				encodeURIComponent(userid) + 
				"?fields=" + 
				encodeURIComponent(
						"entry(title,link,gphoto:id,gphoto:access,media:*)"
				) + authkey;			
		},
		
		viewAlbumURL: function(userid, albumid, authkey) {
			if (authkey != null && authkey.length > 0)
				authkey = "&authkey=" + authkey;
			else
				authkey = "";
			
			return ImGallery.picasa.BASE_URL + 
				encodeURIComponent(userid) + "/albumid/" + albumid +
				"?fields=" + 
				encodeURIComponent(
						"entry(id,content,title,summary,media:*)"
				) + authkey;				
		}
	},
	
	rss: {
		prefs: {
			URL: "rssFeedURL"
		}
	}
};

//TODO to remove after debug
function dump_ob(obj, indent, te) {
	if (indent > 10) return "STOP";
	
	var tab_t = "&nbsp;&nbsp;&nbsp;";
	
	var tab = "";
	for (var n = 0; n < indent; n++) tab += tab_t;

	te.innerHTML += tab;
	if (typeof(obj) == "object") te.innerHTML += "{<br />";
	
	var p;
	for (var i in obj) {
		if (i == "data") continue;
		p = obj[i];
		te.innerHTML += tab + tab_t + i + ": ";
		if (typeof(p) == "object")
			dump_ob(p, indent + 1, te);
		else 
			te.innerHTML += p + "<br />";
	}

	if (typeof(obj) == "object") te.innerHTML += tab + "}<br />";
}

function dump(obj, target) {
	var t = document.getElementById(target);
	dump_ob(obj, 0, t);
	t.style.display = "block";
}

function getTextNode(node) {
	if (node.childNodes[0])
		return node.childNodes[0].nodeValue;
	else
		return "";
}
/******************************* GLOBS *********************************/


var gPrefs = new gadgets.Prefs();
var picasaAlbums;

function Album(id, title, authkey, thumbnail) {
	this.id = id;
	this.title = title;
	this.authkey = authkey;
	this.thumbnail = thumbnail;
}

function feedTypeChange() {
	var useridRow = document.getElementById("useridRow");
	var picasaWebAlbumRow = document.getElementById("picasaWebAlbumRow");
	var rssRow = document.getElementById("rssRow");
	
	switch(parseInt(document.getElementById("feedType").value)) {
	case ImGallery.feedType.PICASA: 
		useridRow.style.display = "";
		picasaWebAlbumRow.style.display = "";
		rssRow.style.display = "none";
		break;
		
	case ImGallery.feedType.RSS: 
		useridRow.style.display = "none";
		picasaWebAlbumRow.style.display = "none";
		rssRow.style.display = "";
		break;
	}
	
	update();
}

function useridChange() {
	picasaClearAlbums();
}

function picasaParseEntry(e) {
	var album_id = null;
	var auth_key = null;
	var title = null;
	var thumbnail = null;
	var priv = false;
	
	for (var x = 0; x < e.childNodes.length; x++) {
		var n = e.childNodes[x];
		
		if (n.nodeName == "gphoto:id")
			album_id = getTextNode(n);
		else if (n.nodeName == "title")
			title = getTextNode(n);
		else if (n.nodeName == "gphoto:access")
			priv = ( getTextNode(n) == "private" );
		else if (n.nodeName == "media:group") {
			var ts = n.getElementsByTagName("media:thumbnail");
			if (ts != null && ts.length > 0) 
				thumbnail = ts.item(0).getAttribute("url");
		} else if (n.nodeName == "link" && n.getAttribute("rel") == "http://schemas.google.com/g/2005#feed") { 
			var href = n.getAttribute("href");
			var p = href.indexOf("?authkey=");
			if (p > -1) 
				auth_key = href.substr(p + 9);
		} 
	}
	
	/*if (priv && auth_key == null) 
		return null;
	else*/
	var ret = new Album(album_id, title, auth_key, thumbnail);
	return ret;
}

function picasaAddAlbum(album, selected) {
	var albumSel = document.getElementById("picasaWebAlbumId");
	var op = document.createElement("OPTION");
	op.value = album.id;
	op.text = album.title;
	op.selected = selected;
	albumSel.appendChild(op);	
}

function picasaClearAlbums() {
	var albumSel = document.getElementById("picasaWebAlbumId");
	albumSel.innerHTML = "";
	document.getElementById("picasaWebAlbumThumb").src = "";
	document.getElementById("picasaWebAlbumSelect").style.display = "none";
}

function picasaUpdateSelectedAlbum() {
	var albumSel = document.getElementById("picasaWebAlbumId");
	if (albumSel.length == 0) return;

	var a = picasaAlbums[albumSel.selectedIndex];
	if (a.thumbnail != null) 
		document.getElementById("picasaWebAlbumThumb").src = a.thumbnail;
}

function picasaParseAlbums(obj) {

	try {
		
		//dump(obj, "debugDIV");
		
		var ac = 0;
		picasaAlbums = new Array();

		var root = obj.data.getElementsByTagName("feed").item(0);
		
		var selectedID = gPrefs.getString(ImGallery.picasa.prefs.ALBUM_ID);
		
		for (var i = 0; i < root.childNodes.length; i++) {
			if (root.childNodes[i].nodeName == "entry") {
				var album = picasaParseEntry(root.childNodes[i]);
				if (album != null) {
					picasaAlbums[ac++] = album;
					picasaAddAlbum(album, (album.id == selectedID));
				}
			}
		}
		
		picasaUpdateSelectedAlbum();
		
		document.getElementById("picasaWebAlbumLoading").style.display = "none";
		document.getElementById("picasaWebAlbumSelect").style.display = "block";

		update();
	} catch(e) {
		document.getElementById("picasaWebAlbumLoading").innerHTML = e.message;	
	}
}

function picasaFetchAlbums() {
	var url = ImGallery.picasa.listAlbumsURL(
			document.getElementById("userid").value, 
			document.getElementById("picasaAuthKey").value
	);

	document.getElementById("picasaWebAlbumLoading").style.display = "block";
	document.getElementById("picasaWebAlbumSelect").style.display = "none";
	picasaClearAlbums();
	
	var params = {};
	params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
	//params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
	//params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
	gadgets.io.makeRequest(url, picasaParseAlbums, params);
	
}

function update() {
	var feedType = parseInt(document.getElementById("feedType").value);
	gPrefs.set(
			ImGallery.prefs.FEED_TYPE, 
			feedType
	);
	
	gPrefs.set(
			ImGallery.prefs.SLIDESHOW, 
			document.getElementById("enableSlideshow").checked
	);
	
	gPrefs.set(
			ImGallery.prefs.SLIDE_INTERVAL, 
			parseInt(document.getElementById("ssInterval").value)
	);
	
	gPrefs.set(
			ImGallery.prefs.SHOW_CAPTION, 
			document.getElementById("showCaption").checked
	);
	
	gPrefs.set(
			ImGallery.prefs.AUTO_HEIGHT, 
			document.getElementById("autoHeight").checked
	);
	
	switch(feedType) {
	case ImGallery.feedType.PICASA:
		var aSel = document.getElementById("picasaWebAlbumId");
		gPrefs.set(
				ImGallery.prefs.USER_ID, 
				document.getElementById("userid").value
		);
		
		if (aSel.length > 0 && aSel.value.length > 0)
			gPrefs.set(ImGallery.picasa.prefs.ALBUM_ID, aSel.value);
		/*var ak = picasaAlbums[aSel.selectedIndex].authkey;
		if (ak == null)
			gPrefs.set("picasaAuthKey", "");
		else
			gPrefs.set("picasaAuthKey", ak);*/
			
		gPrefs.set(
				ImGallery.picasa.prefs.AUTH_KEY, 
				document.getElementById("picasaAuthKey").value
		);
		
		picasaUpdateSelectedAlbum();
		break;
		
	case ImGallery.feedType.RSS:
		gPrefs.set(
				ImGallery.rss.prefs.URL, 
				document.getElementById("feedURL").value
		);
		break;
	}
	
	gadgets.window.adjustHeight();
}

function doInit() {
	try {
		var feedType = gPrefs.getInt(ImGallery.prefs.FEED_TYPE);
		document.getElementById("feedType").value = feedType;
		
		document.getElementById("enableSlideshow").checked = 
			gPrefs.getBool(ImGallery.prefs.SLIDESHOW);
		
		document.getElementById("ssInterval").value = 
			gPrefs.getInt(ImGallery.prefs.SLIDE_INTERVAL);
		
		document.getElementById("showCaption").checked = 
			gPrefs.getBool(ImGallery.prefs.SHOW_CAPTION);
		
		document.getElementById("autoHeight").checked = 
			gPrefs.getBool(ImGallery.prefs.AUTO_HEIGHT);
		
		document.getElementById("userid").value = 
			gPrefs.getString(ImGallery.prefs.USER_ID);

		document.getElementById("feedURL").value = 
			gPrefs.getString(ImGallery.rss.prefs.URL);

		document.getElementById("picasaAuthKey").value = 
			gPrefs.getString(ImGallery.picasa.prefs.AUTH_KEY);
		
		switch(feedType) {
		case ImGallery.feedType.PICASA:
			var uid = gPrefs.getString(ImGallery.prefs.USER_ID);
			//var aid = gPrefs.getString(ImGallery.picasa.prefs.ALBUM_ID);
			if (uid != null && uid.length > 0)
				picasaFetchAlbums();
			break;
		}
		
		feedTypeChange();
	} catch(e) {
		alert(e.message);
	}
}

gadgets.util.registerOnLoadHandler(doInit);

